<template>
  <!-- 登录界面容器 -->
  <view class="login-container">
    <!-- 顶部帮助入口 -->
    <!-- <text class="help-link">帮助</text> -->
    
    <!-- 主内容区 -->
    <view class="main-content">
      <!-- 应用标识 -->
      <text class="app-name">搭搭子</text>
      <text class="slogan">找到你的玩伴</text>
      
      <!-- 登录表单 -->
      <view class="login-form">
        <input class="input-field" v-model="userName" placeholder="请输入用户名" />
        <input class="input-field" v-model="password" placeholder="请输入密码" password />
      </view>
      
      <!-- 操作按钮 -->
      <button class="login-btn" @click="handleLogin">登录账号</button>
      <text class="other-login" @click="goToPage('/pages/starter/new-account/new-account')">注册新账号</text>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { baseUrl , http } from '@/utils/request'

const userName = ref('')
const password = ref('')

const goToPage = (url) => {
  if (!url) return
  uni.navigateTo({
    url
  })
}

const handleLogin = async () => {
  if (!userName.value || !password.value) {
    uni.showToast({
      title: '请输入用户名和密码',
      icon: 'none'
    })
    return
  }

  try {
    // const res = await http.post(`/users/login?userName=${encodeURIComponent(userName.value)}&password=${encodeURIComponent(password.value)}`)
    
    // const res = await http.post(`/users/login?userName=${userName.value}&password=${password.value}`)

    // const res = await uni.request({
    //   url: baseUrl + `/users/login?userName=${userName.value}&password=${password.value}`,
    //   method: 'POST'
    // })

    const res = await uni.request({
      url: baseUrl + '/users/login',
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data:{
        userName:userName.value,
        password:password.value
      }
    })

    console.log('登录响应:', res.data)
    
    if (res.data.code === 500) {
      uni.showToast({
        title: '登录失败，请重试',
        icon: 'none'
      })
      return
    }
    
    // 存储token到本地缓存
    uni.setStorageSync('token', res.data.token)
    uni.setStorageSync('userId', res.data.userId)
    uni.setStorageSync('userName', res.data.userName)
    uni.setStorageSync('loggedIn', res.data.loggedIn)
    
    uni.showToast({
      title: '登录成功',
      icon: 'success'
    })
    
    // 登录成功后跳转
    uni.navigateBack()
  } catch (error) {
    console.error('登录失败:', error)
    uni.showToast({
      title: '登录失败',
      icon: 'error'
    })
  }
}
</script>

<style>
.login-form {
  width: 560rpx;
  margin-bottom: 80rpx;
}

.input-field {
  width: 100%;
  height: 96rpx;
  border: 1rpx solid #eee;
  border-radius: 48rpx;
  padding: 0 32rpx;
  margin-bottom: 32rpx;
  font-size: 28rpx;
}
</style>

<style>
.login-container {
  height: 100vh;
  background: #ffffff;
  position: relative;
}

/* 帮助链接 */
.help-link {
  position: absolute;
  top: 48rpx;
  right: 48rpx;
  color: #666;
  font-size: 28rpx;
}

/* 主内容区 */
.main-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 20vh;
}

/* 应用标识 */
.app-name {
  font-size: 64rpx;
  color: #FF2442;
  font-weight: bold;
  margin-bottom: 16rpx;
}

.slogan {
  font-size: 38rpx;
  color: #666;
  margin-bottom: 120rpx;
}

/* 用户信息 */
.user-avatar {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  margin-bottom: 32rpx;
}

.username {
  font-size: 36rpx;
  color: #333;
  margin-bottom: 80rpx;
}

/* 登录按钮 */
.login-btn {
  background: #FF2442;
  color: #ffffff;
  width: 560rpx;
  height: 96rpx;
  border-radius: 48rpx;
  font-size: 32rpx;
  line-height: 96rpx;
  margin-bottom: 48rpx;
}

.other-login {
  color: #FF2442;
  font-size: 28rpx;
}
</style>
